<?php
// 应用公共文件
namespace common;

class Common
{
    /**
     * url参数转化为数组
     * @param string $query
     * @return mixed
     */
    public static function queryToArray(string $query)
    {
        if (empty($query)) {
            return [];
        }

        $params = explode('&', $query);
        return array_reduce($params, function ($params, $item) {
            $item = explode('=', $item);
            $name = $item[0] ?? '';
            $value = $item[1] ?? '';
            if ($name && $value !== '') {
                $params[$item[0]] = $item[1] ?? '';
            }
            return $params;
        });
    }

    /**
     * @param $password
     * @param null $prefix
     * @return string
     */
    public static function encryptionPassword($password, $prefix = null)
    {
        if (empty($prefix)) {
            $prefix = config('config.password.prefix');
        }

        if (empty($password)) {
            $password = '123456';
        }

        return sha1(md5($prefix . $password));
    }
}